package em;

import base.jfinal.HandlerError;
import base.jfinal.PermissionInterceptor;
import base.jfinal.plugin.*;
import base.util.PropertiesContent;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.fastjson.JSONArray;
import com.jfinal.config.*;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.render.ViewType;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import em.job.ConnServer;
import em.program.imp.OtherData;
import em.program.imp.RealData;
import em.util.GlobalConfig;
import org.apache.log4j.Logger;


public class JFWebConfig extends JFinalConfig {
	private Logger log=Logger.getLogger(getClass());
	public static DruidPlugin druidPlugin;
	/**
	 * 配置常量O
	 */
	@Override
	public void configConstant(Constants me){
		GlobalConfig.getInstance().initProp();
		me.setDevMode(PropertiesContent.getToBool("jfinal.devmode",false));
		me.setViewType(ViewType.OTHER);

		me.setBaseUploadPath(PathKit.getWebRootPath() +"/upload");
		me.setMaxPostSize(PropertiesContent.getToInteger("jfinal.maxpostsize",104857600));//100mb

		ApiConfigKit.setDevMode(me.getDevMode());
	}
	
	@Override
	public void configRoute(Routes me) {
		RoutesUtil.add(me);
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置数据库连接池插件
		druidPlugin = new DruidPlugin(PropertiesContent.get("url"),
									  PropertiesContent.get("username"),
									  PropertiesContent.get("password"),
									  PropertiesContent.get("driver"));
		druidPlugin.set(PropertiesContent.getToInteger("initialSize", 1),
				PropertiesContent.getToInteger("minIdle", 1),
				PropertiesContent.getToInteger("maxActive", 1));
		druidPlugin.addFilter(new StatFilter());
		druidPlugin.start();
		me.add(druidPlugin);
		
		//添加自动绑定model与表插件
		AutoTableBindPlugin autoTableBindPlugin = new AutoTableBindPlugin(druidPlugin, TableNameStyle.LOWER);
		autoTableBindPlugin.setShowSql(true);
		autoTableBindPlugin.setDialect(new MysqlDialect());
		autoTableBindPlugin.setContainerFactory(new OrderContainerFactory());
		me.add(autoTableBindPlugin);
		
		//定时器服务
		JSONArray jsons=RoutesUtil.GetClass();
		QuartzPlugin quartzPlugin=new QuartzPlugin(jsons);
		me.add(quartzPlugin);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new HandlerError());//错误处理
		me.add(new PermissionInterceptor());//权限控制
	}

	@Override
	public void configHandler(Handlers me) {
		DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid");
	    me.add(dvh);
	    //该处理器将request.getContextPath()存储在root中，可以解决路径问题
	  	ContextPathHandler path = new ContextPathHandler("root");
	  	me.add(path);

	}
	
	@Override
	public void afterJFinalStart(){
		//初始化数据库中配置表的数据,并保存到内存中
		GlobalConfig.getInstance().init();
		ConnServer.getInstance();
		OtherData.getInstance();
		RealData.getInstance();
	}

	/**
	 * 整个server断开之前调用此方法
	 */
	@Override
	public void beforeJFinalStop(){

	}
}
